
package com.kanjian.music.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.activity.BeginDNAActivity;
import com.kanjian.music.activity.DownloadActivity;
import com.kanjian.music.activity.MyFavMusicActivity;
import com.kanjian.music.activity.MyFollowMusicianActivity;
import com.kanjian.music.activity.RegisterActivity;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.User;
import com.kanjian.music.network.HttpLoader;
import com.kanjian.music.util.FastBlur;
import com.kanjian.music.util.PlayerUtil;
import com.kanjian.music.view.RecyclingImageView;
import com.kanjian.music.volley.VolleyQueue;

/**
 * 我的
 * 
 * @author xuyanjun@kanjian.com
 */
public class MineFragment extends BaseFragment implements LoaderCallbacks<String> {

    private View mRootView;

    private RecyclingImageView mIv_background;

    private NetworkImageView mIv_minePic;

    private TextView mTv_userName;

    private Button mBtn_dna;

    private Button mBtn_likeMusic;

    private Button mBtn_likeMusician;

    private Button mBtn_downloadedMusic;

    private Button mBtn_myRadio;

    private User mUser = User.getCurrentUser();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            initView(inflater, container);
        }
        return mRootView;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        mRootView = inflater.inflate(R.layout.fragment_mine, container, false);
        mIv_background = (RecyclingImageView) mRootView
                .findViewById(R.id.fragment_mine_header_background);
        mIv_minePic = (NetworkImageView) mRootView.findViewById(R.id.fragment_mine_header_pic);
        mIv_minePic.setDefaultImageResId(R.drawable.default_image_small);
        mTv_userName = (TextView) mRootView.findViewById(R.id.fragment_mine_header_username);
        mBtn_dna = (Button) mRootView.findViewById(R.id.fragment_mine_dna);
        mBtn_likeMusic = (Button) mRootView.findViewById(R.id.fragment_mine_like_music);
        mBtn_likeMusician = (Button) mRootView.findViewById(R.id.fragment_mine_like_musician);
        mBtn_downloadedMusic = (Button) mRootView.findViewById(R.id.fragment_mine_downloaded_music);
        mBtn_myRadio = (Button) mRootView.findViewById(R.id.fragment_mine_my_radio);
        setClickListener();
        checkLogin();
    }

    private void checkLogin() {
        mUser = User.getCurrentUser();
        if (User.isLogin()) {
            processLogin();
        } else {
            processUnLogin();
        }
    }

    private void processLogin() {
        // image
        mIv_minePic.setImageUrl(mUser.getUserPic(), VolleyQueue.getImageLoader());
        VolleyQueue.getImageRequestQueue().add(
                new ImageRequest(mUser.getUserPic(), new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bmp) {
                        mIv_background.setImageBitmap(FastBlur.doBlur(bmp, 50, false));
                    }
                }, 0, 0, Config.ARGB_8888, null));
        // username
        mTv_userName.setText(mUser.getUserName());
        mTv_userName.setClickable(false);
    }

    private void processUnLogin() {
        // image
        mIv_background.setImageBitmap(FastBlur.doBlur(
                BitmapFactory.decodeResource(getResources(), R.drawable.default_image_small), 50,
                false));
        // username
        mTv_userName.setText("点击这里注册");
        mTv_userName.setClickable(true);
        mTv_userName.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), RegisterActivity.class));
            }
        });
    }

    private void setClickListener() {

        // mBtn_loginStatus.setOnClickListener(new OnClickListener() {
        // @Override
        // public void onClick(View v) {
        // if (User.isLogin()) {
        // User.logout(MineFragment.this);
        // } else {
        // startActivityForResult(new Intent(getActivity(),
        // LoginActivity.class), 0);
        // }
        // }
        // });

        mBtn_dna.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), BeginDNAActivity.class));
            }
        });
        mBtn_likeMusic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyFavMusicActivity.class));
            }
        });
        mBtn_likeMusician.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyFollowMusicianActivity.class));
            }
        });
        mBtn_downloadedMusic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), DownloadActivity.class));
            }
        });

        mBtn_myRadio.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle args = new Bundle();
                args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                        ApiRequestManager.getRadioRequest(7));
                getLoaderManager().restartLoader(0, args, MineFragment.this);
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        checkLogin();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new HttpLoader(getActivity(), args);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null) {
            ArrayList<Music> musicList = Music.getMusicListFromJson(data);
            if (musicList != null) {
                PlayerUtil.addTempleMusicListThenPlay(musicList,
                        ((BaseActivity) getActivity()).mPlayService);
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
    }

}
